/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/25
 * Time: 上午7:02
 */
$(function () {
    loadList('/v1/teacher/rent/list.json');
    $('#view-rent-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        var rent_user = button.data('rent-user');
        var rent_time = button.data('rent-time');
        var fee = button.data('fee');
        var remark = button.data('remark');
        $(this).find('.rent-id').val(id);
        $(this).find('.apart-name').text(name);
        $(this).find('.rent-user-name').text(rent_user);
        $(this).find('.rent-time').text(rent_time);
        $(this).find('.rent-fee').text('¥ ' + fee);
        $(this).find('.pay-fee').val(fee);
        $(this).find('.rent-remark').text(remark);
    });
    $('#view-rent-dialog input[name="pay-type"]').on('change', function(){
        var that = $('#view-rent-dialog');
        if ($(this).val() == 2) {
            that.find('.pay-online').removeClass('hidden');
            var fee = that.find('.pay-fee').val();
            pay('/v1/teacher/create/order', 'post', {fee:fee, tag:'rent'}, function(json){
                if (json && json.ret_content) {
                    if (json.ret_code == "0") {
                        $("#qrcode canvas").remove();
                        $("#qrcode").qrcode({
                            render: "canvas", //table方式
                            width: 100, //宽度
                            height: 100, //高度
                            text: json.ret_content.url //任意内容
                        });
                        $('.fee-order').val(json.ret_content.out_trade_no);
                        getResult(json.ret_content.out_trade_no);
                    }
                    else {
                        alert(json.ret_content);
                    }
                }
            });
        } else {
            that.find('.pay-online').addClass('hidden');
        }
    });
    $('#view-rent-dialog').on('hide.bs.modal', function(){
        $(this).find('.rent-id').val('');
        $(this).find('.apart-name').text('');
        $(this).find('.rent-user-name').text('');
        $(this).find('.rent-time').text('');
        $(this).find('.rent-fee').text('');
        $(this).find('.rent-remark').text('');
        $(this).find('input:radio').removeAttr('checked');
        $(this).find('input:radio:checked').prop('checked', false);
    });
    $('#view-rent-dialog .btn-save').on('click', function(){
        var that = $('#view-rent-dialog');
        var data = {
            id: that.find('.rent-id').val(),
            pay_type: that.find('input[name="pay-type"]:checked').val(),
            remark: that.find('.rent-remark').val()
        };
        if (data.pay_type == 2) {
            data.pay_order = that.find('.fee-pay-order').val();
        }
        if (checkForm(data)) {
            sendAjax('/v1/teacher/rent/change/pay', 'post', {data:data}, function(json){
                that.modal('hide');
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#view-pay-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        var rent_user = button.data('rent-user');
        var rent_time = button.data('rent-time');
        var fee = button.data('fee');
        var pay_type = getPayStatusType(button.data('pay-type'));
        var remark = button.data('remark');
        var pay_at = button.data('pay-at');
        var pay_order = button.data('pay-order');
        $(this).find('.rent-id').val(id);
        $(this).find('.apart-name').text(name);
        $(this).find('.rent-user-name').text(rent_user);
        $(this).find('.rent-time').text(rent_time);
        $(this).find('.rent-fee').text('¥ ' + fee);
        $(this).find('.rent-remark').text(remark);
        $(this).find('.pay-type').html(pay_type);
        $(this).find('.pay-at').text(pay_at);
        $(this).find('.pay-order').text(pay_order);
    });
    $('#view-pay-dialog').on('hide.bs.modal', function(){
        $(this).find('.rent-id').val('');
        $(this).find('.apart-name').text('');
        $(this).find('.rent-user-name').text('');
        $(this).find('.rent-time').text('');
        $(this).find('.rent-fee').text('');
        $(this).find('.rent-remark').text('');
        $(this).find('.pay-type').html('');
        $(this).find('.pay-at').text('');
        $(this).find('.pay-order').text('');
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url) {
    $("#rent-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'rent_user_name',
            title: '承租人姓名（工号）'
        }, {
            field: 'rent_time',
            title: '时间范围'
        }, {
            field: 'fee',
            title: '月租金',
            formatter: function (value) {
                return '¥ ' + value;
            }
        }, {
            field: 'pay_type',
            title: '支付状态',
            formatter: function (value) {
                return getPayType(value);
            }
        }, {
            field: 'pay_at',
            title: '支付时间',
            visible: false
        }, {
            field: 'pay_order',
            title: '支付订单',
            visible: false
        }, {
            field: 'status',
            title: '状态',
            visible: false
        }, {
            field: 'remark',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-rent" data-toggle="modal" data-target="#view-rent-dialog" data-id="'+row.id+'" data-name="'+row.name+'" data-rent-user="'+row.rent_user_name+'" data-rent-time="'+row.rent_time+'" data-fee="'+row.fee+'" data-remark="'+value+'">详情</a>';
                if (row.status == 2) {
                    action = '<a class="btn btn-xs btn-primary view-pay" data-toggle="modal" data-target="#view-pay-dialog" data-id="'+row.id+'" data-name="'+row.name+'" data-rent-user="'+row.rent_user_name+'" data-rent-time="'+row.rent_time+'" data-fee="'+row.fee+'" data-pay-type="'+row.pay_type+'" data-remark="'+value+'" data-pay-at="'+row.pay_at+'" data-pay-order="'+row.pay_order+'">查看支付信息</a>';
                }
                return action;
            }
        }]
    });
}

function getPayType(payType) {
    switch (payType) {
        case 0 : return '<span class="text-default"><b>--</b></span>';
        case 1 : return '<span class="text-warning"><b>现金支付</b></span>';
        case 2 : return '<span class="text-success"><b>支付宝支付</b></span>';
        case 3 : return '<span class="text-success"><b>银行卡转账</b></span>';
        case 4 : return '<span class="text-success"><b>工资扣除</b></span>';
        default : return '<span class="text-default"><b>--</b></span>';
    }
}

function getPayStatusType(pay_type) {
    switch (pay_type) {
        case 1 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用现金支付</span>';
        case 2 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用支付宝支付</span>';
        case 3 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用银行卡转账支付</span>';
        case 4 : return '<span class="text-warning"><i class="fa fa-check-circle"></i>已用工资扣除支付</span>';
    }
}

function getResult(order_id) {
    var interval = setInterval(function () {
        pay('/v1/teacher/select/order.json', 'get', {order_id: order_id, tag:'rent'}, function(json){
            if (json && json.ret_content) {
                if (json.ret_code == "0") {
                    if(json.ret_content.status == 2){
                        clearInterval(interval);
                        $('.fee-order').val('');
                        $('.fee-pay-order').val(order_id);
                        alert("订单支付成功");
                    }
                }
            }
        });
    },5000);
}

function checkForm(data) {
    if (!data.id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.pay_type) {
        $('#action-tip .modal-body .tip-content').text('请选择支付方式');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}